package hash;

import java.util.HashSet;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description: 202.快乐数
 * User: Li_yizYa
 * Date: 2025—03—26
 * Time: 22:15
 */
public class Solution3 {
    public static void main(String[] args) {
        int num = 19;
        System.out.println(isHappy(num));
    }
    private static boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (n != 1 && !set.contains(n)) {
            set.add(n);
            n = getNextNumber(n);
            System.out.println();
        }
        return n == 1;
    }

    private static int getNextNumber(int n) {
        int ret = 0;
        while (n > 0) {
            int tmp = n % 10;
            ret += tmp * tmp;
            n /= 10;
        }
        return ret;
    }
}
